//
//  SEGExcellentStaffDetailViewController.m
//  SEGUhomePropertyBaseService
//
//  Created by Clavis on 2018/12/11.
//  Copyright © 2018 刘山. All rights reserved.
//

#import "SEGExcellentStaffDetailViewController.h"
#import "SEGExcellentStaffDetailCollectionViewCell.h"
#if __has_include(<Common/SEGPageSizeCustomerFlowLayout.h>)
#import <Commom/SEGPageSizeCustomerFlowLayout.h>
#else
#import "SEGPageSizeCustomerFlowLayout.h"
#endif
#if __has_include(<Common/SEGPageSizeCustomerCollectionView.h>)
#import <Common/SEGPageSizeCustomerCollectionView.h>
#else
#import "SEGPageSizeCustomerCollectionView.h"
#endif

#import <SEGViewModel/SEGExcellentStaffViewModel.h>

#define kExcellentStaffCellID @"SEGExcellentStaffDetailCollectionViewCell"

@interface SEGExcellentStaffDetailViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) SEGPageSizeCustomerFlowLayout *flowLayout;
@property (nonatomic, strong) SEGPageSizeCustomerCollectionView *collectionView;
@property (nonatomic, strong) SEGExcellentStaffViewModel *viewModel;

@end

@implementation SEGExcellentStaffDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    WeakSelf;
    dispatch_async(dispatch_get_main_queue(), ^{
        if (weakSelf.selectIndex < weakSelf.dataSource.count) {
            [weakSelf.collectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:weakSelf.selectIndex inSection:0] animated:NO scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
            CGFloat scrollviewWith = weakSelf.flowLayout.itemSize.width;
            [weakSelf.collectionView.placeholderSV setContentOffset:CGPointMake(weakSelf.selectIndex * scrollviewWith, 0) animated:NO];
        }
    });
}

- (void)setupUI {
    self.view.backgroundColor = UIColor.clearColor;
    
    UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithEffect:blurEffect];
    visualView.frame = self.view.bounds;
    visualView.alpha = 0.9;
    [self.view addSubview:visualView];
    
    self.collectionView = ({
        SEGPageSizeCustomerFlowLayout *layout = [[SEGPageSizeCustomerFlowLayout alloc] init];
        BOOL isIphone5 = [UIScreen mainScreen].bounds.size.width == 320;
        CGFloat itemWidth = LYIsiPhoneX() ? LYScreenWidth() - 70 : LYScreenWidth() - 80;
        CGFloat itemHeight = isIphone5 ? LYScreenHeight() - 126 : (LYIsiPhoneX() ? LYScreenHeight() - 246 : LYScreenHeight() - 166);
        layout.itemSize = CGSizeMake(itemWidth, itemHeight);
        layout.minimumInteritemSpacing = 1;
        layout.minimumLineSpacing = 1;
        self.flowLayout = layout;
        SEGPageSizeCustomerCollectionView *collectionView = [[SEGPageSizeCustomerCollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.backgroundColor = UIColor.clearColor;
        [collectionView registerClass:[SEGExcellentStaffDetailCollectionViewCell class] forCellWithReuseIdentifier:kExcellentStaffCellID];
        [self.view addSubview:collectionView];
        collectionView;
    });
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    UIButton *closeButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_fabu_close")] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(closeButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
        [self.view addSubview:button];
        button;
    });
    [closeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(LYIsiPhoneX() ? 60 : 35);
        make.left.mas_equalTo(35);
        make.width.height.mas_equalTo(44);
    }];
    
    UILabel *tipLabel = ({
        UILabel *lable = [[UILabel alloc] init];
        [self.view addSubview:lable];
        lable.textColor = UIColor.whiteColor;
        lable.font = kFont_11;
        lable.text = @"注：每人一天限点赞一次";
        lable;
    });
    [tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.offset(0);
        make.bottom.mas_equalTo(LYIsiPhoneX() ? -70 : -25);
    }];
}

- (void)closeButtonTap:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - <UICollectionViewDelegate, UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    self.collectionView.placeholderSV.contentSize = CGSizeMake(self.flowLayout.itemSize.width * self.dataSource.count, 0);
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SEGExcellentStaffDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kExcellentStaffCellID forIndexPath:indexPath];
    [cell updateUIWithModel:self.dataSource[indexPath.row]];
    WeakSelf;
    cell.faverTapBlock = ^{
        [weakSelf faverEmployeeWith:indexPath];
    };
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat scrollviewWith = self.flowLayout.itemSize.width;
    [self.collectionView.placeholderSV setContentOffset:CGPointMake(indexPath.item * scrollviewWith, 0) animated:YES];
}

#pragma mark - handle
- (void)faverEmployeeWith:(NSIndexPath *)indexPath {
    SEGExcellentStaffModel *faveredStaffModel = self.dataSource[indexPath.row];
    RACSignal *favorSignal = [self.viewModel requestFavorSignalWithEmplyeeModel:faveredStaffModel];
    if (!favorSignal) {
        [SEGAlertView showTmepMessageWithMessage:@"每天只能点赞一次！" withtime:0.3];
        return;
    }
    
    WeakSelf;
    [SVProgressHUD show];
    [favorSignal subscribeNext:^(SEGExcellentStaffModel *  _Nullable staffModel) {
        [SVProgressHUD dismiss];
        [SEGAlertView showTmepMessageWithMessage:@"感谢你的支持" withtime:0.3];
        [weakSelf.collectionView reloadData];
        // 修改当前数据 & 发送通知修改员工列表和物业评优列表的数据
        [[NSNotificationCenter defaultCenter] postNotificationName:kExcellentStaffFaverStateUpdateNotification object:staffModel];
    } error:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription withtime:0.3];
    }];
}

- (SEGExcellentStaffViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[SEGExcellentStaffViewModel alloc] init];
    }
    return _viewModel;
}

@end
